package com.liuya.db;

import java.lang.reflect.Method;
import java.util.Properties;

public class DataSourceProviderMethodImpl extends DataSource implements java.io.Serializable {
	private String name;
	private javax.sql.DataSource dataSource;
	private Properties prop;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public javax.sql.DataSource getDataSource() {
		return dataSource;
	}

	public void setup(Properties prop) {
		this.prop = prop;
		String clazz = prop.getProperty("class");
		String method = prop.getProperty("method");

		try {
			Class implClass = Class.forName(clazz);
			Method m = implClass.getMethod(method, new Class[] {});
			dataSource = (javax.sql.DataSource) m.invoke(implClass.newInstance(), new Object[] {});
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public String getValidInfo(Properties prop) {
		this.prop = prop;
		if (prop.containsKey("class") && prop.containsKey("method")) {
			return null;
		} else {

			return "Required properties: class,method";
		}
	}

	public String toString() {
		return "PoweredBy JavaMethod [" + prop.getProperty("class") + "." + prop.getProperty("method") + "();]";
	}
}
